package org.shs.company.action;

import static org.shs.common.config.SystemInitializer.app_msgs;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.shs.common.config.BaseDispatchAction;
import org.shs.common.constants.AppConstants;
import org.shs.company.form.CompanyForm;
import org.shs.company.model.CompanyModel;
import org.shs.company.service.CompanyService;

/**
 * Handles the user interaction coming in from the company centre screen
 * <br/>
 * <br/><b>Created:</b>&nbsp;&nbsp; 19-Nov-2008 23:53:03
 *
 * @author anirvan
 *
 */
public class CompanyAction extends BaseDispatchAction
{
   
   /**
    * The service class instance for this module
    */
   private static final CompanyService serviceCls = new CompanyService();
   
   /**
    * Retrieves the various collectors registered with the system.
    * Also handles a search request for a particular string value amongst
    * registered companies.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward initCompanyList(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.company-main");
      CompanyForm formData = (CompanyForm) form;
      
      // TODO : Pagination to be done
      request.setAttribute("companyList", 
                           serviceCls.getCompanies(formData.getCompanySrch(),
                                                   formData.getSrchInactive()));
      
      return mapping.findForward(sForward);
   }
   
   
   /**
    * De-actvates/Activates the companys data from the database.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward updActiveStatus(ActionMapping mapping, 
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.company-main");
      CompanyForm formData = (CompanyForm) form;
      boolean status = false;
      
      // de-activate/Activate the selected companies
      status = serviceCls.updActiveStatus(formData.getChkCompanyId(), 
                                             formData.getActive());
      // Retrieve the list of companies
      request.setAttribute("companyList", serviceCls.getCompanies(null, 
                                                                  AppConstants.FALSE_STR));
      // Set the delete status for appropriate message
      if (status)
      {
         request.setAttribute(AppConstants.SUCCESS_MSG, 
                              app_msgs.getMessage("success.updStatus.company"));
      }
      else
      {
         request.setAttribute(AppConstants.FAILURE_MSG, 
                              app_msgs.getMessage("err.updStatus.company"));
      }
      
      
      return mapping.findForward(sForward);
   }
   
   /**
    * Retrieves the details about a particular company
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward getCompanyDetails(ActionMapping mapping, 
                                          ActionForm form,
                                          HttpServletRequest request,
                                          HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.company-view");
      CompanyForm formData = (CompanyForm)form;
      
      // Retrieve the company details
      request.setAttribute("company", 
                           serviceCls.getCompanyDetails(formData
                                                            .getCompanyId()));
      
      return mapping.findForward(sForward);
   }
   
   /**
    * Retrieves the details about a particular company for the edit screen.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward editCompany(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.company-add-edit");
      CompanyForm formData = (CompanyForm) form;
      
      // Retrieve the company details
      request.setAttribute("company", serviceCls.getCompanyDetails(
                                                   formData.getCompanyId()));
      // Also fetch the list of all states
//      request.setAttribute("states", serviceCls.getAggrWasteTypes());
      
      return mapping.findForward(sForward);
   }
   
   /**
    * Saves the company data into the database.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward saveCompany(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.company-add-edit");
      CompanyForm formData = (CompanyForm) form;
      CompanyModel companyDetails = null;
      
      formData.setUserId(userData.getId());
      if (formData.getCompanyId() == null 
               || formData.getCompanyId().length() == 0)
      {
         // Insert the new company details
         companyDetails = serviceCls.addCompanyDetails(formData);
      }
      else
      {
         // Update the existing waste type details
         companyDetails = serviceCls.editCompanyDetails(formData);
         
      }
      // Check if all the tasks actually got done properly
      if (companyDetails != null)
      {
         companyDetails.setMessage(AppConstants.SUCCESS_MSG);
      }
      else
      {
         companyDetails = new CompanyModel();
         companyDetails.setMessage(AppConstants.FAILURE_MSG);
      }
      
      // Set the waste details as the request attrib
      request.setAttribute("company", companyDetails);
      // Also fetch the list of all states
//      request.setAttribute("aggrWasteTypes", serviceCls.getAggrWasteTypes());
      
      
      return mapping.findForward(sForward);
   }
   
   /**
    * Retrieves the data to display the add screen.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward addCompany(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.company-add-edit");
      
      CompanyForm formData = (CompanyForm) form;
      formData.setCompanyName(formData.getCompanySrch());
      // Also fetch the list of all states
//      request.setAttribute("aggrWasteTypes", serviceCls.getAggrWasteTypes());
      
      return mapping.findForward(sForward);
   }
}
